package top.timous.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
//开启swagger2
@EnableSwagger2
public class SwaggerConfig {


    //多个人可以配置多个分组，这样就可以进行协同开发
    @Bean
    public Docket docket1(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("a");

    }
    @Bean
    public Docket docket2(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("b");

    }
    @Bean
    public Docket docket3(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("c");

    }
    @Bean
    public Docket docket4(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("d");

    }


    //swagger的bean实例
    @Bean
    public Docket docket(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev" , "test");
        //获取项目的配置环境
        //判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);




        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("陈留红")
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
                //Predicate<RequestHandler> selector配置要扫描的接口的方式
                //basePackage指定要扫描的包
                //any()扫描全部
                //none()都不扫描
                //withClassAnnotation("Controller.class")扫描类上的注解    Controller 只扫描带有controller 的接口
                //withMethodAnnotation()  扫描方法的注解
                .apis(RequestHandlerSelectors.basePackage("com.timous.controller"))
                .build();//build工厂模式

    }

    //配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        return new ApiInfo("陈留红的swaggerApi文档",
                "这个作者很nb",
                "v1.0",
                "http://timous.top",
                //作者信息
                new Contact("陈留红", "http://timous.top", "1844736251@qq.com"),
                "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
